package leetcode.code1573;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	

	int mod = (int) 1e9 + 7;

	public int numWays(String s) {
		char[] cs = s.toCharArray();
		int cnt = 0, n = cs.length;
		for (int i = 0; i < cs.length; i++) {
			cnt += cs[i] == '1' ? 1 : 0;
		}
		long ans = 0;
		if (cnt == 0) {
			ans = 1l * (n - 1) * (n - 2) / 2 % mod;
		} else if ((cnt % 3) != 0) {
			return 0;
		} else {
			int one = cnt / 3;
			int p1 = this.f1(cs, 0, one);
			int z1 = this.f0(cs, p1);
			int p2 = this.f1(cs, p1 + z1, one);
			int z2 = this.f0(cs, p2);
			ans = 1l * (z1 + 1) * (z2 + 1) % mod;
		}
		return (int) ans;
	}
	// 连续0的数量
	private int f0(char[] cs, int start) {
		int cnt = 0;
		for (int i = start; i < cs.length && cs[i] == '0'; i++) {
			cnt++;
		}
		return cnt;
	}
	// 找到one个1，最后第一个0的位置
	private int f1(char[] cs, int start, int one) {
		int i = start;
		for (int cnt = 0; i < cs.length && cnt < one; i++) {
			cnt += cs[i] == '1' ? 1 : 0;
		}
		return i;
	}


	@Override
	public void debug4() {
		H.compare(4, this.numWays("10101"));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}

